package com.simple.javatraining.testing.testshell;

import java.util.List;
import java.util.Scanner;

import com.simple.javatraining.testing.Users.User;
import com.simple.javatraining.testing.checker.Checker;
import com.simple.javatraining.testing.examinator.Examinator;
import com.simple.javatraining.testing.io.InputOutput;
import com.simple.javatraining.testing.io.Logger;
import com.simple.javatraining.testing.questionsformer.HardcodedQuestionsFormer;
import com.simple.javatraining.testing.shuffler.Shuffler;
import com.simple.javatraining.testing.testinside.answers.Answer;
import com.simple.javatraining.testing.testinside.questions.Question;

public class Tester {
    private HardcodedQuestionsFormer hardcodedQuestionsFormer;
    private Shuffler shuffler;
    private Examinator examinator;
    private Checker checker;
    private Scanner in;
    private List<Question> allQuestions;
    private User user;

    public Tester() {
        this.hardcodedQuestionsFormer = HardcodedQuestionsFormer.getHardcodedQuestions();
        this.shuffler = new Shuffler();
        this.examinator = new Examinator();
        this.checker = new Checker();
        this.in = new Scanner(System.in);
        this.user = new User(in);
    }

    public static void main(String[] args) {
        Tester tester = new Tester();
        tester.run();
    }

    public void run() {
        prepareTest();
        testUser();
        outputResults();
        in.close();
    }

    private void prepareTest() {
        allQuestions = hardcodedQuestionsFormer.getAllQuestions();
        shuffler.shuffleData(allQuestions);
    }

    private void testUser() {
        for(Question currentQuestion:allQuestions) {
            examinator.showTaskToUser(currentQuestion);
            Answer usersAnswer = user.getAnswer(in);
            double result = currentQuestion.checkUserAnswerForCorrect(usersAnswer);
            checker.addCurrentResultToList(result, currentQuestion);
        }
    }

    private void outputResults() {
        double resultInPercents = checker.calculateSummaryResultInPercents();
        InputOutput.outputResult(resultInPercents);
        Logger logger = new Logger(user.getUserName(), resultInPercents);
        logger.addInfoToLog();
        System.out.println("gg");
    }
}
